<?php

class ImagesController extends AppController {
  public $helpers = array('Html', 'TWSForm');
  public $components = array('Session', 'Util', 'Upload');
  public $name = 'Images';

  function uploadWYMContentImage() {
    $imageUploaded = '';
    if ($this->request->is('post')) {
      $view = new View($this);
      $html = $view->loadHelper('Html');
      //getting file name
      $image = $this->request->params["form"]["wym_img_select"];
      if (!empty($image)) {
        $fileName = $this->Util->cleanFileName($image["name"], true)."_".date("U");
        $imageUploaded = $this->Upload->uploadImage($image, IMAGES . '/' . Configure::read('App.imageUploadFolder'), null, null, $fileName);
      }
    }
    $result = "";
    if(!empty($imageUploaded)){
      $imgLink = $html->image(Configure::read('App.imageUploadFolder')."/".$imageUploaded);
      $imgLink= substr(stristr($imgLink ,'"'),1);
      $result = substr($imgLink, 0, strpos($imgLink, '"'));
    }
    $alt = isset($this->request->data["wym_alt"]) ? $this->request->data["wym_alt"] : "";
    $title = isset($this->request->data["wym_title"]) ? $this->request->data["wym_title"] : "" ;
    $align = isset($this->request->data["wym_align_radio"]) ? $this->request->data["wym_align_radio"] : "";

    $json = array('fileUploaded'=> $result, 'alt'=> $alt, 'title'=> $title ,  'align'=> $align );
    $this->set('json',$json);
    $this->render('/Elements/json', '/json/default');
  }

  function index() {}

  function uploadPhotoImages($albumId) {
    //TODO - Verify that albumId is valid
    $this->loadModel('Photo');
    $result = array();
    if ($this->request->is('post')) {
      $view = new View($this);
      $html = $view->loadHelper('Html');
      //getting file name
      $images = $this->request->params["form"]["fileUpload1"];
      if (!empty($images)) {
        $imgCount = sizeof($images['name']);
        $albumsFolder = Configure::read('App.defaultAlbumsImageFolder');
        for ($count = 0; $count < $imgCount; $count++) {
          $data = array("Photo" => array());
          $fileName = $albumId . '_' . $this->Util->cleanFileName($images["name"][$count], true) . "_" . date("U") . '_' . $count;
          $imageObject = array("name" => $images["name"][$count], "type" => $images["type"][$count], "tmp_name" => $images["tmp_name"][$count], "error" => $images["error"][$count], "size" => $images["size"][$count]);
          $imageUploaded = $this->Upload->uploadImage($imageObject, WWW_ROOT . '/' . Configure::read('App.imageFolder') . '/' . $albumsFolder, null, null, $fileName, true, Configure::read('App.photoImageThumbWidth'));
          $data["Photo"]["image_path"] = $albumsFolder . '/' . $imageUploaded;
          $data["Photo"]["image_thumb_path"] = $albumsFolder . '/' . Configure::read('App.defaultThumbnailPrefix') . $imageUploaded;
          $data["Photo"]["album_id"] = $albumId;
          if ($this->Photo->save($data, false)) {
            //success
            $photoResult = array("success" => true, "name" => $images["name"][$count], "uploadedName" => $imageUploaded, "albumId"=>$albumId,  "errorMessage" => "");
          } else {
            //fail
            $photoResult = array("success" => false, "name" => $images["name"][$count], "uploadedName" => "", "albumId"=>$albumId,  "errorMessage" => $images["error"][$count]);
          }          
          array_push($result, $photoResult);
          $this->Photo->id = null;
        }
      }
    }

    $json = array('result' => $result);
    $this->set('json', $json);
    $this->render('/Elements/json', '/json/default');
  }


    function uploadAdImages($adId) {
    //TODO - Verify that albumId is valid
    $this->loadModel('AdPhoto');
    $result = array();
    if ($this->request->is('post')) {
      $view = new View($this);
      $html = $view->loadHelper('Html');
      //getting file name
      $images = $this->request->params["form"]["fileUpload1"];
      if (!empty($images)) {
        $imgCount = sizeof($images['name']);
        $albumsFolder = Configure::read('App.adsImageFolder');
        for ($count = 0; $count < $imgCount; $count++) {
          $data = array("Photo" => array());
          $fileName = $adId . '_' . $this->Util->cleanFileName($images["name"][$count], true) . "_" . date("U") . '_' . $count;
          $imageObject = array("name" => $images["name"][$count], "type" => $images["type"][$count], "tmp_name" => $images["tmp_name"][$count], "error" => $images["error"][$count], "size" => $images["size"][$count]);
          $imageUploaded = $this->Upload->uploadImage($imageObject, WWW_ROOT . '/' . Configure::read('App.imageFolder') . '/' . $albumsFolder, Configure::read('App.adsImageWidth'), Configure::read('App.adsImageHeight'), $fileName, true, Configure::read('App.photoImageThumbWidth'));
          $data["AdPhoto"]["image_path"] = $albumsFolder . '/' . $imageUploaded;
          $data["AdPhoto"]["image_thumb_path"] = $albumsFolder . '/' . Configure::read('App.defaultThumbnailPrefix') . $imageUploaded;
          $data["AdPhoto"]["ad_id"] = $adId;
          try {
            if ($this->AdPhoto->save($data, false)) {
              //success
              $photoResult = array("success" => true, "name" => $images["name"][$count], "uploadedName" => $imageUploaded, "adId" => $adId, "errorMessage" => "");
            } else {
              //fail
              $photoResult = array("success" => false, "name" => $images["name"][$count], "uploadedName" => "", "adId" => $adId, "errorMessage" => $images["error"][$count]);
            }
          } catch (Exception $e) {
            $photoResult = array("success" => false, "name" => $images["name"][$count], "uploadedName" => "", "adId" => $adId, "errorMessage" => $e->getMessage());
          }
          array_push($result, $photoResult);
          $this->AdPhoto->id = null;
        }
      }
    }

    $json = array('result' => $result);
    $this->set('json', $json);
    $this->render('/Elements/json', '/json/default');
  }


  function uploadHomepagePhotoImages() {
    $this->loadModel('HomepagePhoto');
    $result = array();
    if ($this->request->is('post')) {
      $view = new View($this);
      $html = $view->loadHelper('Html');
      //getting file name
      $images = $this->request->params["form"]["fileUpload1"];
      if (!empty($images)) {
        $imgCount = sizeof($images['name']);
        $albumsFolder = Configure::read('App.homepageImageFolder');
        for ($count = 0; $count < $imgCount; $count++) {
          $data = array("Photo" => array());
          $fileName =  $this->Util->cleanFileName($images["name"][$count], true) . "_" . date("U") . '_' . $count;
          $imageObject = array("name" => $images["name"][$count], "type" => $images["type"][$count], "tmp_name" => $images["tmp_name"][$count], "error" => $images["error"][$count], "size" => $images["size"][$count]);
          $imageUploaded = $this->Upload->uploadImage($imageObject, WWW_ROOT . '/' . Configure::read('App.imageFolder') . '/' . $albumsFolder, Configure::read("App.homepageImageWidth"), Configure::read("App.homepageImageHeight"), $fileName, true, Configure::read('App.photoImageThumbWidth'));
          $data["HomepagePhoto"]["image_path"] = $albumsFolder . '/' . $imageUploaded;
          $data["HomepagePhoto"]["image_thumb_path"] = $albumsFolder . '/' . Configure::read('App.defaultThumbnailPrefix') . $imageUploaded;
          if ($this->HomepagePhoto->save($data, false)) {
            //success
            $photoResult = array("success" => true, "name" => $images["name"][$count], "uploadedName" => $imageUploaded, "errorMessage" => "");
          } else {
            //fail
            $photoResult = array("success" => false, "name" => $images["name"][$count], "uploadedName" => "", "errorMessage" => $images["error"][$count]);
          }
          array_push($result, $photoResult);
          $this->HomepagePhoto->id = null;
        }
      }
    }

    $json = array('result' => $result);
    $this->set('json', $json);
    $this->render('/Elements/json', '/json/default');
  }


}

?>
